package com.example.teach.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.teach.entity.Academy;
import com.example.teach.entity.Major;
import com.example.teach.entity.Result;
import com.example.teach.service.AcademyService;
import com.example.teach.service.MajorService;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;
import java.util.List;

import static com.fasterxml.jackson.databind.type.LogicalType.Collection;
//学院
@RestController
@RequestMapping("/academy")
public class AcademyController {

    @Resource
    private AcademyService academyService;

    @Resource
    private MajorService majorService;

    @DeleteMapping("/deletemajor")
    public Result deleteMajor(@RequestParam("academyId") Integer academyId,
                              @RequestParam("majorId") Integer majorId) {
        academyService.deletemajor(academyId,majorId);
        majorService.updateStatusToZero(majorId);
        return Result.success();
    }

    /**
     * 根据学院id查询有哪些专业
     * @param id
     * @return
     */
    @GetMapping("/selectmajor")
    public Result selectMajor(@RequestParam Integer id) {
        List<Integer> majorIdList =  academyService.selectmajor(id);
        if(majorIdList == null || majorIdList.isEmpty()){
            return Result.success(Collections.emptyList());
        }
        List<Major> majorList = majorService.listByIds(majorIdList);
        return Result.success(majorList);
    }

    /**
     * 给学院添加专业
     * @param academyId
     * @param majorId
     * @return
     */
    @PostMapping("/majorjoinacademy")
    public Result join(@RequestParam Integer academyId,
                       @RequestParam Integer majorId){
        if(academyService.select(academyId,majorId) != null){
            return Result.error("该学院已有相关专业");
        }
        academyService.create(academyId,majorId);
        majorService.updatestatus(majorId);
        return Result.success();
    }

    @PostMapping("/create")
    public Result create(@RequestBody Academy academy) {
        if(academyService.exists(new LambdaQueryWrapper<>(academy))) {
            return Result.error("学院已存在");
        }
        academyService.save(academy);
        return Result.success(academy);
    }

    @DeleteMapping("/delete")
    public Result deleteById(@RequestParam Integer id) {
        if(academyService.isNotEmpty(id)) {
            return Result.error("该学院有专业，暂无法删除");
        }
        academyService.removeById(id);
        return Result.success();
    }

    @PutMapping("/update")
    public Result update(@RequestBody Academy academy) {
        academyService.updateById(academy);
        return Result.success();
    }

    @GetMapping("/page")
    public Result page(@RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum,
                       @RequestParam(value = "pageSize",defaultValue = "10") Integer pageSize,
                       @RequestParam(value = "academyName",defaultValue = "") String academyName) {
        LambdaQueryWrapper<Academy> queryWrapper = new LambdaQueryWrapper<>();
        if(StrUtil.isNotBlank(academyName)){
            queryWrapper.like(Academy::getAcademyName,academyName);
        }
        Page<Academy> page = academyService.page(new Page<>(pageNum,pageSize), queryWrapper);
        return Result.success(page);
    }

}
